package view
{
	import data.Globals;
	import data.MonsterProp;
	import data.WaveProp;
	
	import events.EventType;
	import events.GameDispatherEvent;
	import events.GameEvent;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import manager.*;
	import manager.MapConfig;
	import manager.ObjectPool;
	
	import utils.GameUtils;
	
	import view.base.BaseView;
	import view.base.ClearView;
	import view.monster.AMonster;
	import view.monster.BaseMonster;
	
	public class MonsterView extends ClearView
	{
		public var aMonster:Array;
		
		private var queue:Vector.<MonsterProp>;
		
		/**
		 *距离上一波怪兽出现时间 
		 */
		private var waveGap:Number;
		/**
		 * 当前一波怪兽 
		 */
		private var curWave:WaveProp;
		
		private var curProp:MonsterProp;
		
		/**
		 *第几波 
		 */
		private var waveNum:int = 1;
		
		private var monster_go:uint;
		
		private var wave_gap:uint;
		
		private var animation_go:uint;
		
		/**
		 * 是否满足检测升级条件 
		 */
		private var isCanCheckUpCondition:Boolean;
		
		public function MonsterView()
		{
			super();
			aMonster = []; 
		}
		
		public function initMonster():void
		{
			if(!queue){
				getQueue();
				waitWaveGap();
				trace("第"+waveNum+"波！");
				return;
			}else if(!queue.length){
				clearTimeout(monster_go);
				waveNum++;
				getQueue();
				if(curWave){
					waitWaveGap();
				}else{
					clearTimeout(wave_gap);
					//当前等级怪物队列结束，检测升级条件
					isCanCheckUpCondition = true;
					return;
				}
				trace("第"+waveNum+"波！");
				return;
			}
			curProp = queue.shift();
			monster_go = setTimeout(monsterGo,curProp.gapTime*1000);
		}
		
		override public function clearView():void{
			super.clearView();
			aMonster.length = 0;
			queue = null;
			clearTimeout(monster_go);
			clearTimeout(wave_gap);
			clearTimeout(animation_go);
			waveNum = 1;
			curWave = null;
			curProp = null;
		}
		
		private function waitWaveGap():void{
			wave_gap = setTimeout(initMonster,waveGap*1000);
			animation_go = setTimeout(playAnimation,(waveGap-3)*1000);
		}
		
		private function playAnimation():void{
			trace("waveNum"+waveNum);
			Globals.gameView.playAnimation(AnimationView.getType(waveNum));
		}

		private function getQueue():void{
			curWave = QueueManage.getInstance().getQueue();
			if(curWave){
				waveGap = curWave.gapTime;
				queue = curWave.list;
			}
		}
		
		/**
		 *创建怪物出口 
		 * 
		 */
		private function monsterGo():void
		{
			var a:BaseMonster = ObjectPool.getObj(MonsterName.getMonsterCls(curProp.name)) as BaseMonster;
			this.addChild(a);
			a.onRemove = deleteThis;
			a.name = curProp.name;
			a.blood = curProp.blood;
			a.speed = curProp.speed;
			//怪物重置
			a.resetData();
			aMonster.push(a);
			initMonster();
		}
		
		private function deleteThis(item:BaseMonster):void
		{
			aMonster.splice(aMonster.indexOf(item),1);
		}
		
		override protected function loop(event:Event):void{
			for each(var monster:BaseMonster in aMonster){
				monster.move();
			}
			if(isCanCheckUpCondition){
				if(aMonster.length == 0){
					isCanCheckUpCondition = false;
					if(Globals.totalLevel&&Globals.totalLevel == LevelManange.getInstance().curLevel){
						Globals.towerWar.gameWin();
					}else{	
						LevelManange.getInstance().curLevel++;
						LevelManange.getInstance().userLevel++;
						Globals.towerWar.gameUpLevel(false);
					}
				}
			}
		}
	}
}